// 发布到npm
// npm publish --registry https://registry.npmjs.org

console.log('zdz-cli is running!')
console.log('zdz-cli is running!arr', [1, 2, 3, 4, 5, 6].slice(3))

// test 1
// const program = require('commander');
// program.version('0.0.1','-v,--version')
//   .parse(process.argv); // process.argv就是用户在命令行中传入的参数

// test 2
const program = require('commander');
const { version } = require('./utils/constants');
// const { version } = require('./utils/constants/index.js');
// program.version(version)
//   .parse(process.argv);

// test 3
// const program = require('commander');
const path = require("path")
const actionsMap = {
  create: { // 创建模板
    des: 'create project',
    alias: 'cr',
    examples: [
      'quick-cli create <template-name>',
    ],
  },
  config: { // 配置配置文件
    des: 'config info',
    alias: 'c',
    examples: [
      'quick-cli config get <k>',
      'quick-cli config set <k> <v>',
    ],
  },
  '*': {
    des: 'command not found',
  },
};
// 循环创建命令
Object.keys(actionsMap).forEach((action) => {
  program
  .command(action) // 命令的名称
  .description(actionsMap[action].des) // 命令的描述
  .action(() => { // 动作
    if (action === '*') { // 如果动作没匹配到说明输入有误
      console.log(acitonMap[action].description);
    } else { // 引用对应的动作文件 将参数传入
      // require(path.resolve(__dirname, action))(...process.argv);
      require(path.resolve(__dirname, action))(...process.argv.slice(3));
      // console.log('process.argv', process.argv) // 可以看下第一个和第二个参数
      // process.argv [
      //   'F:\\Program Files\\nodejs\\node.exe',
      //   'C:\\Users\\Administrator\\AppData\\Roaming\\npm\\node_modules\\zdz-cli\\bin\\www',
      //   'create',
      //   'p',
      //   'ppp',
      //   '22',
      //   '556'
      // ]
    }
  })
  .alias(actionsMap[action].alias) // 命令的别名
});

program.version(version, "-v,--version")
  .parse(process.argv);

program.on('--help', () => {
  console.log('Examples');
  Object.keys(actionsMap).forEach((action) => {
    (actionsMap[action].examples || []).forEach((example) => {
      console.log(`${example}`);
    });
  });
});